﻿using ElectronicShoppingMall.Service.Data;
using ElectronicShoppingMall.Service.Models.Enterprise;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElectronicShoppingMall.Service.Services.Enterprise
{
    public class SystemsAnalysisService : IDisposable
    {
        private ElectronicShoppingMallContext data;
        public SystemsAnalysisService()
        {
            data = new ElectronicShoppingMallContext();
        }
        public void Dispose()
        {
            data.Dispose();
        }

        public List<int> GetSalesSituation()
        {
            List<int> result = new List<int>();
            int sales = 0, refunds = 0, returns = 0;
            sales = data.Orders.Where(o => o.State == 9).Sum(o => o.Count);
            refunds = data.Orders.Where(o => o.State == 7).Sum(o => o.Count);
            returns = data.Orders.Where(o => o.State == 4).Sum(o => o.Count);
            result.Add(sales);
            result.Add(refunds);
            result.Add(returns);
            return result;
        }

        public List<int> GetSalesByCategory()
        {
            List<int> result = new List<int>();
            int men = 0, women = 0, children = 0;
            var orders = data.Orders.Where(o => o.State == 9).ToList();
            foreach (var order in orders)
            {
                var goods = data.GoodsInfoes.SingleOrDefault(g => g.GoodsId == order.GoodsId);
                if (goods.Category == "男士")
                {
                    men += order.Count;
                }
                else if (goods.Category == "女士")
                {
                    women += order.Count;
                }
                else
                {
                    children += order.Count;
                }
            }
            result.Add(men);
            result.Add(women);
            result.Add(children);
            return result;
        }

        public List<int> GetLoginSituation(int employeeId)
        {
            List<int> result = new List<int>();
            var employee = data.Employees.SingleOrDefault(e => e.EmployeeId == employeeId);
            var currentTime = DateTime.Now;


            result.Add(employee.EmployeeLoginRecords.Where(e => e.LoginTime.DayOfWeek == DayOfWeek.Monday && IsInSameWeek(e.LoginTime, currentTime)).Count());
            result.Add(employee.EmployeeLoginRecords.Where(e => e.LoginTime.DayOfWeek == DayOfWeek.Tuesday && IsInSameWeek(e.LoginTime, currentTime)).Count());
            result.Add(employee.EmployeeLoginRecords.Where(e => e.LoginTime.DayOfWeek == DayOfWeek.Wednesday && IsInSameWeek(e.LoginTime, currentTime)).Count());
            result.Add(employee.EmployeeLoginRecords.Where(e => e.LoginTime.DayOfWeek == DayOfWeek.Thursday && IsInSameWeek(e.LoginTime, currentTime)).Count());
            result.Add(employee.EmployeeLoginRecords.Where(e => e.LoginTime.DayOfWeek == DayOfWeek.Friday && IsInSameWeek(e.LoginTime, currentTime)).Count());
            return result;
        }

        public List<int> GetUserActivity(int employeeId)
        {
            List<int> result = new List<int>();
            int monday = 0, tuesday = 0, wednesday = 0, thursday = 0, friday = 0;
            var employee = data.Employees.SingleOrDefault(e => e.EmployeeId == employeeId);
            monday += employee.EmployeeLoginRecords.Where(e => e.LoginTime.DayOfWeek == DayOfWeek.Monday).Count();
            tuesday += employee.EmployeeLoginRecords.Where(e => e.LoginTime.DayOfWeek == DayOfWeek.Tuesday).Count();
            wednesday += employee.EmployeeLoginRecords.Where(e => e.LoginTime.DayOfWeek == DayOfWeek.Wednesday).Count();
            thursday += employee.EmployeeLoginRecords.Where(e => e.LoginTime.DayOfWeek == DayOfWeek.Thursday).Count();
            friday += employee.EmployeeLoginRecords.Where(e => e.LoginTime.DayOfWeek == DayOfWeek.Friday).Count();
            result.Add(monday);
            result.Add(tuesday);
            result.Add(wednesday);
            result.Add(thursday);
            result.Add(friday);
            return result;
        }
        private bool IsInSameWeek(DateTime dtmS, DateTime dtmE)
        {
            TimeSpan ts = dtmE - dtmS;
            double dbl = ts.TotalDays;
            int intDow = Convert.ToInt32(dtmE.DayOfWeek);
            if (intDow == 0) intDow = 7;
            if (dbl >= 7 || dbl >= intDow) return false;
            else return true;
        }

    }
}
